package xuegao.study.mall.web.ms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.ms.Item;
import xuegao.study.mall.common.ms.SeckillRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 秒杀
 */
@RestController
@RequestMapping("/api")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @GetMapping("/items")
    public CommonResult getItems() {
        List<Item> items = seckillService.getItems();
        return CommonResult.success(items);
    }

    @PostMapping("/seckill")
    public CommonResult seckill(@RequestBody SeckillRequest request) {
        //1.先从缓存秒杀
        boolean success = seckillService.seckill(request.getItemId(), request.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        if(!success) {
            map.put("success", false);
            map.put("message", "库存不足");
        }
        return CommonResult.success(map);
    }

}
